package com.practice.text;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Base64.Decoder;

public class ConvertEncode {

	private static final int BUFFER_LENGTH = 0x400;
	
	private static final String OLD_FILE_PATH = "";
	
	private static final String NEW_FILE_PATH = "";
	
	public static void main(String[] args) {
		File srcFile = new File(OLD_FILE_PATH);
		File destFile = new File(NEW_FILE_PATH);
		
		try {
			convertBase64ToBinary(srcFile, destFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void convertBase64ToBinary(File srcFile, File destFile) throws IOException {
		char[] buffer = new char[BUFFER_LENGTH];
		StringBuilder srcTextBuilder = new StringBuilder();
		
		try (FileReader fileReader = new FileReader(srcFile)) {
			int readLength = -1;
			if ((readLength = fileReader.read(buffer, 0, BUFFER_LENGTH)) != -1) {
				srcTextBuilder.append(buffer, 0, readLength);
			}
		}
		
		String srcText = srcTextBuilder.toString();
		
		byte[] bytes = decodeBase64ToByte(srcText);
		
		try (OutputStream output = new FileOutputStream(destFile)) {
			output.write(bytes);
			output.flush();
		}
	}
	
	private static byte[] decodeBase64ToByte(String src) {
		Decoder decoder = Base64.getDecoder();
		return decoder.decode(src);
	}

}
